package Reminder.domain;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.time.LocalTime;
import java.util.ArrayList;

public class Schedule {
    private ArrayList<Course> courseList;
    private ArrayList<Period> periodsList;
    private int periodTimeSpan;
    private int intervalSpan;
    private LocalTime morningFirstPeriodTime;
    private LocalTime afternoonFirstPeriodTime;
    private LocalTime eveningFirstPeriodTime;
    private int morningPeriodSum;
    private int afternoonPeriodSum;
    private int eveningPeriodSum;

    public Schedule(int periodTimeSpan, int intervalSpan, LocalTime morningFirstPeriodTime, LocalTime afternoonFirstPeriodTime, LocalTime eveningFirstPeriodTime, int morningPeriodSum, int afternoonPeriodSum, int eveningPeriodSum) {
        this.periodTimeSpan = periodTimeSpan;
        this.intervalSpan = intervalSpan;
        this.morningFirstPeriodTime = morningFirstPeriodTime;
        this.afternoonFirstPeriodTime = afternoonFirstPeriodTime;
        this.eveningFirstPeriodTime = eveningFirstPeriodTime;
        this.morningPeriodSum = morningPeriodSum;
        this.afternoonPeriodSum = afternoonPeriodSum;
        this.eveningPeriodSum = eveningPeriodSum;
    }
    public static  Schedule parseJSON(JSONObject jsonObject){
        Schedule schedule = new Schedule(JSONObject.parseObject(jsonObject.getString("setting")));
        schedule.setCourseList(JSONObject.parseArray(jsonObject.getString("courseList")));
        schedule.setPeriodsList(JSONObject.parseArray(jsonObject.getString("periodList")));
        return schedule;
    }
    public Schedule(JSONObject setting) {
        setSetting(setting);
    }
    public void setSetting(JSONObject setting){
        this.periodTimeSpan = setting.getInteger("periodTimeSpan");
        this.intervalSpan = setting.getInteger("intervalSpan");
        this.morningFirstPeriodTime = LocalTime.parse(setting.getString("morningFirstPeriodTime"));
        this.afternoonFirstPeriodTime = LocalTime.parse(setting.getString("afternoonFirstPeriodTime"));
        this.eveningFirstPeriodTime = LocalTime.parse(setting.getString("eveningFirstPeriodTime"));
        this.morningPeriodSum = setting.getInteger("morningPeriodSum");
        this.afternoonPeriodSum = setting.getInteger("afternoonPeriodSum");
        this.eveningPeriodSum = setting.getInteger("eveningPeriodSum");
    }

    public void setCourseList(JSONArray courseList){
        this.courseList = new ArrayList<Course>();
        System.out.println(courseList.toString());
        for (Object o : courseList){
            System.out.println(((JSONObject)o).toString());
            this.courseList.add(Course.parseJSON((JSONObject) o));
        }
    }
    public Course getCourseByName(String name){
        for (Course course : courseList) {
            System.out.println(course.toString());
            if (course.getName().equals(name)) {
                return course;
            }
        }
        return null;
    }

    public JSONObject exportSetting(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("periodTimeSpan",periodTimeSpan);
        jsonObject.put("intervalSpan",intervalSpan);
        jsonObject.put("morningFirstPeriodTime",morningFirstPeriodTime);
        jsonObject.put("afternoonFirstPeriodTime",afternoonFirstPeriodTime);
        jsonObject.put("eveningFirstPeriodTime",eveningFirstPeriodTime);
        jsonObject.put("morningPeriodSum",morningPeriodSum);
        jsonObject.put("afternoonPeriodSum",afternoonPeriodSum);
        jsonObject.put("eveningPeriodSum",eveningPeriodSum);
        return jsonObject;
    }
    public JSONArray exportCourseList(){
        JSONArray jsonArray = new JSONArray();
        for(Course course:courseList){
            jsonArray.add(course.toJSON());
        }
        return jsonArray;
    }
    public JSONArray exportPeriodList(){
        JSONArray jsonArray = new JSONArray();
        for(Period period:periodsList){
            jsonArray.add(period.toJSON());
        }
        return jsonArray;
    }
    public JSONObject toJSON(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("setting",exportSetting());
        jsonObject.put("courseList",exportCourseList());
        jsonObject.put("periodList",exportPeriodList());
        return  jsonObject;
    }
    public void getData(JSONArray periods){
        for (Object period : periods) {
            this.periodsList.add(Period.parseJSON((JSONObject) period));
        }
    }
    public boolean existCourse(String name){
        for (Course course : courseList) {
            System.out.println(course.toString());
            if (course.getName().equals(name)) {
                return true;
            }
        }
        return false;
    }
    public LocalTime getAfternoonFirstPeriodTime() {
        return afternoonFirstPeriodTime;
    }

    public void setAfternoonFirstPeriodTime(LocalTime afternoonFirstPeriodTime) {
        this.afternoonFirstPeriodTime = afternoonFirstPeriodTime;
    }

    public LocalTime getEveningFirstPeriodTime() {
        return eveningFirstPeriodTime;
    }

    public void setEveningFirstPeriodTime(LocalTime eveningFirstPeriodTime) {
        this.eveningFirstPeriodTime = eveningFirstPeriodTime;
    }

    public int getAfternoonPeriodSum() {
        return afternoonPeriodSum;
    }

    public void setAfternoonPeriodSum(int afternoonPeriodSum) {
        this.afternoonPeriodSum = afternoonPeriodSum;
    }

    public int getEveningPeriodSum() {
        return eveningPeriodSum;
    }

    public void setEveningPeriodSum(int eveningPeriodSum) {
        this.eveningPeriodSum = eveningPeriodSum;
    }
    public int getIntervalSpan() {
        return intervalSpan;
    }

    public void setIntervalSpan(int intervalSpan) {
        this.intervalSpan = intervalSpan;
    }

    public ArrayList<Course> getCourseList() {
        return courseList;
    }

    public void setCourseList(ArrayList<Course> courseList) {
        this.courseList = courseList;
    }

    public int getPeriodTimeSpan() {
        return periodTimeSpan;
    }

    public void setPeriodTimeSpan(int periodTimeSpan) {
        this.periodTimeSpan = periodTimeSpan;
    }

    public LocalTime getMorningFirstPeriodTime() {
        return morningFirstPeriodTime;
    }

    public void setMorningFirstPeriodTime(LocalTime morningFirstPeriodTime) {
        this.morningFirstPeriodTime = morningFirstPeriodTime;
    }
    public int getMorningPeriodSum() {
        return morningPeriodSum;
    }

    public void setMorningPeriodSum(int morningPeriodSum) {
        this.morningPeriodSum = morningPeriodSum;
    }
    public ArrayList<Period> getPeriodsList() {
        return periodsList;
    }

    public void setPeriodsList(JSONArray periodsList) {
        this.periodsList = new ArrayList<Period>();
        for (Object o : periodsList) {
            JSONObject jsonObject = (JSONObject) o;
            this.periodsList.add(Period.parseJSON(jsonObject));
        }
    }


}
